package com.sanyang.pda.ui.manualsampling;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.sanyang.pda.adapter.QualityDetailAdapter;
import com.sanyang.pda.bean.CauseBean;
import com.sanyang.pda.bean.QualityDetailBean;
import com.sanyang.pda.bean.ReasonListBean;
import com.sanyang.pda.contract.QualityDetailContract;
import com.sanyang.pda.dialog.AddCauseDialog;
import com.sanyang.pda.dialog.ConfrimDialog;
import com.sanyang.pda.presenter.QualityDetailPresenter;
import com.sanyang.pda.utils.StringConfig;
import com.sanyang.pda.utils.UserUtils;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AppActivityKey.QUALITYDETAILACTIVITY)
public class QualityDetailActivity extends BaseMvpActivity<QualityDetailPresenter> implements QualityDetailContract.View {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.title)
    FrameLayout title;
    @BindView(R.id.tv_sample_size)
    TextView tvSampleSize;
    @BindView(R.id.tv_qualified_num)
    TextView tvQualifiedNum;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.tv_add_reason)
    TextView tvAddReason;
    @BindView(R.id.tv_quality_completed)
    TextView tvQualityCompleted;
    String samplingNo;
    QualityDetailAdapter adapter;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @Override
    public QualityDetailPresenter getPresenter() {
        return new QualityDetailPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_quality_detail;
    }

    @Override
    public void initView() {
        adapter = new QualityDetailAdapter(R.layout.item_quality_detail, new ArrayList<>());
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        rvList.setAdapter(adapter);
    }

    @Override
    public void initLinsenterner() {
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter1, @NonNull View view, int position) {
                if (view.getId() == R.id.iv_close) {
                    ConfrimDialog confrimDialog = new ConfrimDialog(mContext, new ConfrimDialog.Onclick() {
                        @Override
                        public void onConfirm() {
                            presenter.deleteOneDetail(adapter.getData().get(position).getSamplingResultNo());
                        }
                    });
                    confrimDialog.show(getString(R.string.querenshanchu));
                }
            }
        });
    }

    @Override
    public void getData() {
        Bundle bundle = getIntent().getExtras();
        samplingNo = bundle.getString(StringConfig.CODE);
        presenter.palletizingInfo(samplingNo);
        presenter.selectType();
    }


    @OnClick(R.id.iv_back)
    public void onIvBackClicked() {
        finishActivity();
    }

    @OnClick(R.id.tv_add_reason)
    public void onTvAddReasonClicked() {

        AddCauseDialog dialog = new AddCauseDialog(mContext, new AddCauseDialog.OnCauseclick() {
            @Override
            public void onCauseClick(String unqualifiedNum, String reason) {
                CauseBean causeBean = new CauseBean();
                causeBean.setSamplingNo(samplingNo);
                causeBean.setUnqualifiedNum(unqualifiedNum);
                causeBean.setReason(reason);
                causeBean.setCheckName(UserUtils.getInstance().getUserBean().getUserName());
                presenter.addReasonSave(causeBean);
            }
        });
        dialog.show(reasonListBeans);
    }

    @OnClick(R.id.tv_quality_completed)
    public void onTvQualityCompletedClicked() {
        presenter.qualityInspectionCompleted(samplingNo);
    }

    @Override
    public void setData(QualityDetailBean qualityDetailBean) {
        tvTitle.setText(qualityDetailBean.getArticleNameAndSpec());
        tvSampleSize.setText(qualityDetailBean.getSamplingNum()+"");
        tvQualifiedNum.setText(qualityDetailBean.getQualifiedNum()+"");
        adapter.setNewInstance(qualityDetailBean.getUnqualifiedInfoList());
    }

    @Override
    public void delectaddSuccess() {
        presenter.palletizingInfo(samplingNo);
    }
    List<ReasonListBean> reasonListBeans;
    @Override
    public void setReasonList(List<ReasonListBean> reasonListBeans) {
        this.reasonListBeans=reasonListBeans;
    }

}
